Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports Cforge.Security.Certificates

Namespace gvImap.Security
	''' <summary>
	''' Form used for selecting available certificates.
	''' </summary>
	Partial Public Class CertProvider
		Inherits Form
		Private _certs() As X509Certificate ' available certificates.
		Private _selectedCertificate As X509Certificate ' selected certificate object.

		''' <summary>
		''' Gets the selected certificate object.
		''' </summary>
		Public ReadOnly Property SelectedCertificate() As X509Certificate
			Get
				Return _selectedCertificate
			End Get
		End Property

		''' <summary>
		''' Initializes a new instance of the class.
		''' </summary>
		Public Sub New()
			InitializeComponent()
		End Sub

		''' <summary>
		''' Loads certificates and update form controls.
		''' </summary>
		''' <param name="certs"></param>
		Public Sub LoadData(ByVal certs() As X509Certificate)
			_certs = certs

			' Add to the combo box.
			For i As Integer = 0 To certs.Length - 1
				cbCertList.Items.Add(certs(i).SubjectName)
			Next i

			If certs.Length > 0 Then
				cbCertList.SelectedIndex = 0

				Dim cert As X509Certificate = certs(0)

				' Update label controls.
				lblOrganizationText.Text = cert.Issuer.OrganizationName
				lblUnitText.Text = cert.Issuer.UnitName
				lblLocationText.Text = String.Format("Country Code: {0}, State: {1}, City: {2}", cert.Issuer.CountryCode, cert.Issuer.State, cert.Issuer.City)
				lblCommonNameText.Text = cert.Issuer.CommonName

				lblOrganizationSubText.Text = cert.Subject.OrganizationName
				lblUnitSubText.Text = cert.Subject.UnitName
				lblLocationSubText.Text = String.Format("Country Code: {0}, State: {1}, City: {2}", cert.Subject.CountryCode, cert.Subject.State, cert.Subject.City)
				lblCommonNameSubText.Text = cert.Subject.CommonName

				lblFromText.Text = cert.EffectiveDate.ToString()
				lblToText.Text = cert.ExpiryDate.ToString()
			End If
		End Sub

		''' <summary>
		''' Handles the certificate list combobox's SelectedIndexChanged event.
		''' </summary>
		Private Sub cbCertList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbCertList.SelectedIndexChanged
			If cbCertList.SelectedIndex <> -1 Then ' a valid certificate is selected.
				Dim selIndex As Integer = cbCertList.SelectedIndex
				Dim c As X509Certificate = _certs(selIndex) ' get from the cached list.

				lblOrganizationText.Text = c.IssuerName ' refresh organization name, subtext
				lblOrganizationSubText.Text = c.SubjectName

				lblFromText.Text = c.EffectiveDate.ToString() ' refresh from and to date.
				lblToText.Text = c.ExpiryDate.ToString()
			End If
		End Sub

		''' <summary>
		''' Handles the OK button's Click event.
		''' </summary>
		Private Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOk.Click
			If cbCertList.SelectedIndex <> -1 Then
				_selectedCertificate = _certs(cbCertList.SelectedIndex)
			Else
				_selectedCertificate = Nothing
			End If

			Close()
		End Sub
	End Class
End Namespace